<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" width="800px">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="160px">
      <el-form-item label="测评票名称" prop="cppName">
        <el-input style="width:90%" v-model="dataForm.cppName" placeholder="请选择测评票名称"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="mark">
        <el-input style="width:90%" v-model="dataForm.mark" placeholder="请输入备注"></el-input>
      </el-form-item>
      <el-form-item label="选择测评项" prop="xmxxName">
        <el-select v-model="dataForm.xmxxName" placeholder="请选择选择测评项" @change="pfxChange" @visible-change="cshPfx"
                   style='width:90%;'>
          <el-option v-for="item in options.pfx" :key="item.id" :label="item.xmName" :value="item.xmName">
          </el-option>
        </el-select>
        <!-- <el-button type="primary" plain @click="obtainPfx()" class="marginLeft">获取最新评分项</el-button> -->
      </el-form-item>
      <el-form-item label="选择评分规则" prop="gzxxName">
        <el-select v-model="dataForm.gzxxName" placeholder="请选择选择评分规则" @change="pfgzChange" @visible-change="cshPfgz"
                   style='width:90%;'>
          <el-option v-for="item in options.pfgz" :key="item.id" :label="item.gzName" :value="item.gzName">
          </el-option>
        </el-select>
        <!-- <el-button type="primary" plain @click="obtainPfgz()" class="marginLeft">获取最新评分规则</el-button> -->
      </el-form-item>
      <el-form-item label="选择测评关系" prop="cpgzName">
        <el-select v-model="dataForm.cpgzName" placeholder="请选择选择测评关系" @change="cpgzChange" @visible-change="cshCpgz"
                   style='width:90%;'>
          <el-option v-for="item in options.cpgz" :key="item.id" :label="item.gzName" :value="item.gzName">
          </el-option>
        </el-select>
        <!-- <el-button type="primary" plain @click="obtaincpgz()" class="marginLeft">获取最新测评规则</el-button> -->
      </el-form-item>
      <el-form-item label="选择评价等次" prop="djxxName">
        <el-select v-model="dataForm.djxxName" placeholder="请选择选择评价等次" @change="cpdjChange" @visible-change="cshCpdj"
                   style='width:90%;'>
          <el-option v-for="item in options.cpdj" :key="item.id" :label="item.djName" :value="item.djName">
          </el-option>
        </el-select>
        <!-- <el-button type="primary" plain @click="obtainCpdj()" class="marginLeft">获取最新评价等次</el-button> -->
      </el-form-item>
      <el-form-item label="测评等级权重" prop="djxxDjqz" v-if="dataForm.djxxName != ''">
        <el-input-number v-model="dataForm.djxxDjqz" :precision="2" :step="1"   :min="0" style="width:200px"></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      <el-button type="primary" plain @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
/*!
 * 绩效考核-测评票管理编辑
 *
 * @author zmk
 * @date 2021-06-22 09:11:35
 */
import {
  getObj,
  addObj,
  putObj,
  selPfxList,
  selPfgzList,
  selCpgzList,
  selCpdjList,
  selCppzbList
} from '@/api/ceping/cepingcpppypzb'
import {getXmTree} from '@/api/ceping/cepingxmmxb'
import {querySubRuleByPid} from '@/api/ceping/cepinggzmxb'
import {querySubRuleByDjId} from '@/api/ceping/cepingdjmxb'
import {getBcpmxb} from '@/api/ceping/cepingcpgzbcpmxb'
import {getCepingMx} from '@/api/ceping/cepingcpgzcpmxb'

export default {
  data() {
    return {
      options: {
        pfx: [],
        pfgz: [],
        cpgz: [],
        cpdj: []
      },
      visible: false,
      canSubmit: false,
      dataForm: {
        id: 0,
        cppName: '',
        xmxxName: '',
        xmxxConfig: '',
        xmxxXmmx: '',
        gzxxName: '',
        gzxxConfig: '',
        gzxxGzmx: '',
        cpgzName: '',
        cpgzBpfdx: '',
        cpgzConfig: '',
        cpgzBcpmxb: '',
        cpgzCpmxb: '',
        cpgzCpmxbCppz: '',
        djxxName: '',
        djxxConfig: '',
        djxxDjmx: '',
        djxxDjqz: 0,
        xmZt: '',
        mark:''
      },
      dataRule: {
        cppName: [
          {required: true, message: '测评票名称不能为空', trigger: 'blur'},
          {min: 1, max: 200, message: '长度在 1 到 200 个字符', trigger: 'blur'}
        ],
         mark: [
          // {required: true, message: '备注不能为空', trigger: 'blur'},
          {min: 0, max: 20, message: '长度在 0 到 20 个字符', trigger: 'blur'}
        ],
        xmxxName: [
          {required: true, message: '测评项不能为空', trigger: 'blur'}
        ],
        gzxxName: [
          {required: true, message: '评分规则不能为空', trigger: 'blur'}
        ],
        cpgzName: [
          {required: true, message: '测评关系不能为空', trigger: 'blur'}
        ],
        djxxDjqz: [
          {required: true, message: '测评等级权重不能为空', trigger: 'blur'},
          {
            validator: function (rule, value, callback) {
              if (value) {
                var tip = "";
                if (value * 1 < 0 || value * 1 > 50) {
                  tip = "测评等级权重应在0-50之间"
                }
                if (tip) {
                  callback(new Error(tip));
                } else {
                  callback();
                }
              } else {
                callback();
              }
            }, trigger: "blur"
          }
        ]
      }
    }
  },
  mounted() {
  },
  methods: {
    //触发登记下拉
    cshCpdj(bz) {
      if (bz) {
        this.dataForm.djxxName = "";
        this.dataForm.djxxConfig = "";
        this.dataForm.djxxDjmx = "";
        this.dataForm.djxxDjqz = 0;
      }
    },
    //触发规则下拉
    cshCpgz(bz) {
      if (bz) {
        this.dataForm.cpgzName = "";
        this.dataForm.cpgzBpfdx = "";
        this.dataForm.cpgzConfig = "";
        this.dataForm.cpgzBcpmxb = "";
        this.dataForm.cpgzCpmxb = "";
        this.dataForm.cpgzCpmxbCppz = "";
      }
    },
    //触发规则下拉获取
    cshPfgz(bz) {
      if (bz) {
        this.dataForm.gzxxName = "";
        this.dataForm.gzxxConfig = "";
        this.dataForm.gzxxGzmx = "";
      }
    },
    //触发项下拉获取
    cshPfx(bz) {
      if (bz) {
        this.dataForm.xmxxName = "";
        this.dataForm.xmxxConfig = "";
        this.dataForm.xmxxXmmx = "";
      }
    },
    //评分项选择后触发事件
    pfxChange(name) {
      var obj = this.options.pfx.find((item) => {
        return item.xmName === name;
      });
      this.dataForm.xmxxConfig = JSON.stringify(obj);
      getXmTree(obj.id).then(response => {
        this.dataForm.xmxxXmmx = JSON.stringify(response.data.data);
      })
    },
    //评分规则选择后触发事件
    pfgzChange(name) {
      var obj = this.options.pfgz.find((item) => {
        return item.gzName === name;
      });
      this.dataForm.gzxxConfig = JSON.stringify(obj);
      querySubRuleByPid(obj.id).then(response => {
        this.dataForm.gzxxGzmx = JSON.stringify(response.data.data);
      })
    },
    //测评规则选择后触发事件
    cpgzChange(name) {
      var obj = this.options.cpgz.find((item) => {
        return item.gzName === name;
      });
      this.dataForm.cpgzConfig = JSON.stringify(obj);
      this.dataForm.cpgzBpfdx = obj.gzBpfdx;
      selCppzbList(obj.id).then(response => {
        this.dataForm.cpgzCpmxbCppz = JSON.stringify(response.data.data);
      });
      getCepingMx(obj.id).then(response => {
        this.dataForm.cpgzCpmxb = JSON.stringify(response.data.data);
      });
      getBcpmxb(obj.id).then(response => {
        this.dataForm.cpgzBcpmxb = JSON.stringify(response.data.data);
      });
    },
    //评价等次选择后触发事件
    cpdjChange(name) {
      var obj = this.options.cpdj.find((item) => {
        return item.djName === name;
      });
      this.dataForm.djxxConfig = JSON.stringify(obj);
      querySubRuleByDjId(obj.id).then(response => {
        this.dataForm.djxxDjmx = JSON.stringify(response.data.data);
      })
    },
    //测评登记下拉
    obtainCpdj() {
      selCpdjList().then(response => {
        this.options.cpdj = response.data.data;
        // this.dataForm.djxxName = "";
        // this.dataForm.djxxConfig = "";
        // this.dataForm.djxxDjmx = "";
      })
    },
    //测评规则下拉
    obtaincpgz() {
      selCpgzList().then(response => {
        this.options.cpgz = response.data.data;
        // this.dataForm.cpgzName = "";
        // this.dataForm.cpgzBpfdx = "";
        // this.dataForm.cpgzConfig = "";
        // this.dataForm.cpgzBcpmxb = "";
        // this.dataForm.cpgzCpmxb = "";
        // this.dataForm.cpgzCpmxbCppz = "";
      })
    },
    //评分规则下拉获取
    obtainPfgz() {
      selPfgzList().then(response => {
        this.options.pfgz = response.data.data;
        // this.dataForm.gzxxName = "";
        // this.dataForm.gzxxConfig = "";
        // this.dataForm.gzxxGzmx = "";
      })
    },
    //评分项下拉获取
    obtainPfx() {
      selPfxList().then(response => {
        this.options.pfx = response.data.data;
        // this.dataForm.xmxxName = "";
        // this.dataForm.xmxxConfig = "";
        // this.dataForm.xmxxXmmx = "";
      })
    },
    //数据初始化
    initialization() {
      // this.options = {
      //   pfx: [],
      //   pfgz: [],
      //   cpgz: [],
      //   cpdj: []
      // };

      this.obtainPfx();
      this.obtainPfgz();
      this.obtaincpgz();
      this.obtainCpdj();

      this.dataForm = {
        id: 0,
        cppName: '',
        xmxxName: '',
        xmxxConfig: '',
        xmxxXmmx: '',
        gzxxName: '',
        gzxxConfig: '',
        gzxxGzmx: '',
        cpgzName: '',
        cpgzBpfdx: '',
        cpgzConfig: '',
        cpgzBcpmxb: '',
        cpgzCpmxb: '',
        cpgzCpmxbCppz: '',
        djxxName: '',
        djxxConfig: '',
        djxxDjmx: '',
        djxxDjqz: 0,
        xmZt: '',
        mark:''
      };
    },
    init(id) {
      this.initialization();
      this.dataForm.id = id || 0;
      this.visible = true;
      this.canSubmit = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          getObj(this.dataForm.id).then(response => {
            this.dataForm = response.data.data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      if(this.dataForm.gzxxGzmx == '[]'){
        this.$notify.closeAll();
        this.$notify.error({
          // title: '错误',
          message: "评分规则："+ this.dataForm.gzxxName + '没有配置选项，请在评分规则中完善配置',
          showClose: true,
          duration: 5000
        })
        return;
      }
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.id) {
            putObj(this.dataForm).then(data => {
              this.$notify.success('修改成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          } else {
            addObj(this.dataForm).then(data => {
              this.$notify.success('添加成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          }
        }
      })
    }
  }
}
</script>
